//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
 const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
    data: {
        hei2:500,
        active:0,
         isLogin: false,
        page:1,
        tabsArray:[{
                "name": "全部",
                'icon_no':'../../images/d12.png',
                'icon_se':'../../images/order11.png',
                "list": [],
                'active':0,
                "total": 0
            },
            {
               "name": "待付款",
              'icon_no':'../../images/order2.png',
              'icon_se':'../../images/oder22.png',
               "list": [],
                'active':1,
               "total": 0
            },
            
            {
                "name": "待收货",
             'icon_no':'../../images/order4.png',
             'icon_se':'../../images/order44.png',
              "list": [],
               'active':2,
              "total": 0
            },
            {
                 "name": "完成",
            'icon_no':'../../images/order5.png',
            'icon_se':'../../images/order55.png',
             "list": [],
              'active':3,
             "total": 0
            }
        ],
               
    },
   onLoad(options) {
    
     
  
         
   },

   onShow() {
     
   this.setData({
       
        hei2:app.globalData.screenHeight-210,
        
   })
   var token = Store.getStore('userInfo');
   
   
   
     if (token) { 
       this.setData({
       userInfo: Store.getStore('userInfo'),
       isLogin: true
     })
     }
      this.requstList(0,'','',1);   
   
      
      
   },
   goLogin: function() {
       wx.navigateTo({
           url: '../login/index',
       })
   },
   requstList: function(status, orderState,payState, page) {
       if(!this.data.isLogin){
           return  wx.navigateTo({
           url: '../login/index',
       })
       }
       let that = this;
       var i = 0;
       if (status != "0") {
           i = parseInt(status);
       }
        let list = `tabsArray[${i}].list`;
         let total = `tabsArray[${i}].total`;
       if(page == 1){
           this.setData({
               [list]:[],
               [total]:0
           })
       }
     
   
      var data = {
          "asc": true,
          "isChose": true,
          "limit": 10,
          "orderState": orderState,
          "payState":payState,
          "page": page,
          "sidx": ""
      }
   
      
      
   
       Api.orderListApi(data).then(res => {
           console.log(res);
           if (res.code == 0) {
               var arr = res.data.records;
   
               if (arr.length == 0) {
                   return;
               }
               
               
               var oldArr = that.data.tabsArray[i].list;
   
               var newArr = oldArr.concat(arr);
   
               that.setData({
                   [list]: newArr,
                   [total]: res.data.total,
                   isShow: false,
               })
           } else if (res.code == "401") {
               that.setData({
                   isShow: true,
               })
   
           } else {
   
           }
   
       })
   },
   
   // clickDe: function(e) {
   
   //     let i = e.currentTarget.dataset.inde;
   //     let j = this.data.active;
   //     var comSta = `tabsArray[${j}].list[${i}].isSelect`;
   //     let a = this.data.tabsArray[j].list[i].isSelect;
   //     if (a == true) {
   //         this.setData({
   //             [comSta]: false
   //         })
   //     } else {
   //         this.setData({
   //             [comSta]: true
   //         })
   //     }
   //     },
   
   //事件处理函数
   cheakAllOrder(e){
           let a = e.currentTarget.dataset.id;
           this.setData({
               active:a,
           })
        
      
       let list = `tabsArray[${a}].list`;
       this.setData({
           [list]: [],
            page: 1
   
       })
       let paySta = "";
       let orderSta ="";
   
       if (a == "1") {
           paySta = "110901";
           orderSta = '140302'
       }  else if (a == "2") {
          
           paySta = "110903";
           orderSta = '140304'
       }else if (a == "3") {
          paySta = "110903";
          orderSta = '140305'
       }
       this.requstList(a,orderSta,paySta,1)
   
   },
   
   refreshBottom: function(e) {
       var i = e.currentTarget.dataset.index;
     
       var a = "" + e.currentTarget.dataset.index;
       
   
       let paySta = "";
       let orderSta ="";
          
       if (a == "1") {
           paySta = "110901";
           orderSta = '140302'
       } else if (a == "2") {
          
           paySta = "110903";
           orderSta = '140304'
       }else if (a == "3") {
          paySta = "110903";
          orderSta = '140305'
       }
   
       var total = this.data.tabsArray[i].total;
       var list = this.data.tabsArray[i].list;
   
       if (list.length < total) {
           var c = this.data.page;
           c++;
           this.setData({
               page: c
           })
           this.requstList(a,orderSta,paySta,c)
   
       } else {
           this.setData({
               isShowText: i
           })
       }
   
   
   },
   
   SureShouHuo(e){
       // console.log()
        let data = {'ckOrderNo':e.currentTarget.dataset.item}
        Api.confirm_receiptApi(data).then(res=>{
           
            this.requstList('2','140304','110903',1)
        })
   },
   Sure(){
       let that =this;
       let arr = this.data.tabsArray[1].list;
       let NewArray = [];
       arr.forEach(item=>{
           if(item.isSelect){
               NewArray.push(item.ckOrderNo);
           }
       })
       if(NewArray.length == 0){
           wx.showLoading({
               title:'请至少选择一笔订单进行确认!',
               icon:'none'
           })
       }else{
           
            Api.confirm_collectionApi(NewArray).then(res=>{
                
                
                 that.requstList(1,'140302','110901',1)
            })
       }
      
       
   },
  
   Cancel(){
         let that =this;
         let arr = this.data.tabsArray[1].list;
         let NewArray = [];
           arr.forEach(item=>{
               if(item.isSelect){
                   NewArray.push(item.ckOrderNo);
               }
           })
           if(NewArray.length == 0){
               wx.showToast({
                   title:'请至少选择一笔订单进行取消!',
                   icon:'none'
               })
           }else{
               
                Api.confirm_cancelApi(NewArray).then(res=>{
                    
                    
                     that.requstList(1,'140302','110901',1)
                })
           }                  
   },
 
   
});
